<script>

import { createModel, getModel, updateModel } from '@/api/bpm/model'

export default {
  name: 'ModelEdition',
  data() {
    return {
      visible: false,
      dataForm: {
        key: '',
        name: '',
        description: '',
        category: 'system'
      },
      dataRules: {
        key: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      this.visible = true
      this.dataForm.id = null
      const formInstance = this.$refs.dataFormRef
      if (formInstance) {
        formInstance.resetFields()
      }
      if (id) {
        this.getBaseClass(id)
      }
    },
    getBaseClass(id) {
      getModel(id).then(res => {
        Object.assign(this.dataForm, res.data)
      })
    },
    submitHandle() {
      this.$refs.dataFormRef.validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.dataForm.id) {
          updateModel(this.dataForm).then(() => {
            this.$message.success('修改模型成功')
            this.refresh()
          })
        } else {
          createModel(this.dataForm).then(() => {
            this.$message.success('新增模型成功')
            this.refresh()
          })
        }
      })
    },
    refresh() {
      this.$emit('refresh')
    }
  }
}
</script>

<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px">
      <el-form-item prop="key" label="流程标识">
        <el-input v-model="dataForm.key" placeholder="流程标识" />
      </el-form-item>
      <el-form-item prop="name" label="流程名称">
        <el-input v-model="dataForm.name" placeholder="字典名称" />
      </el-form-item>
      <el-form-item label="流程分类" prop="category">
        <el-select v-model="dataForm.category" placeholder="请选择流程分类" clearable style="width: 100%">
          <el-option value="system" prop="system" />
        </el-select>
      </el-form-item>
      <el-form-item prop="description" label="备注">
        <el-input v-model="dataForm.description" placeholder="备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
